﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class JobSiteAreaDAL : IJobSiteAreaDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public JobSiteAreaDAL()
        { 
        
        }        
        public IList<JobSiteAreaInfo> ListJobSiteAreas(JobSiteAreaInfo areaInfo)
        {
            string strSQL;
            Nullable<int> JobSiteSeq = areaInfo.JobSites == null ? 0 : areaInfo.JobSites.Sequence;

            List<JobSiteAreaInfo> infoList = new List<JobSiteAreaInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", areaInfo.Sequence);
                dbManager.AddParameters(2, "@JobSiteSeq", JobSiteSeq);

                strSQL = "SELECT ja.JOB_SITE_AREA_SEQ, ja.JOB_SITE_SEQ, ja.AREA_NAME, ja.REMARKS"
                        + ", ja.RECORD_STATUS, ja.CREATED_BY, ja.UPDATED_BY"
                    + " FROM HRM_MP_JOB_SITE_AREAS ja"
                    + " WHERE ja.RECORD_STATUS not like @DeleteRecord"
                    + (areaInfo.Sequence > 0 ? " AND ja.JOB_SITE_AREA_SEQ = @Seq" : "")
                    + (JobSiteSeq > 0 ? " AND ja.JOB_SITE_SEQ = @JobSiteSeq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    
                    JobSiteDAL jobsiteDAL = new JobSiteDAL();
                    JobSiteInfo jsInfo = new JobSiteInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<JobSiteInfo> jobsiteList = jobsiteDAL.ListJobSite(new JobSiteInfo(rdr.IsDBNull(1) ? 0 : rdr.GetInt32(1)));
                        jsInfo = jobsiteList.Count > 0 ? jobsiteList[0] : null;
                    }
                    JobSiteAreaInfo Info = new JobSiteAreaInfo(rdr.GetInt32(0)
                            , jsInfo
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5)
                            , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6));
                    infoList.Add(Info);
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }
        public DataSet LoadJobSiteAreas(JobSiteAreaInfo areaInfo)
        {
            string strSQL;
            Nullable<int> JobSiteSeq = areaInfo.JobSites == null ? 0 : areaInfo.JobSites.Sequence;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", areaInfo.Sequence);
                dbManager.AddParameters(2, "@JobSiteSeq", JobSiteSeq);

                strSQL = "SELECT ja.JOB_SITE_AREA_SEQ, ja.JOB_SITE_SEQ, ja.AREA_NAME, ja.REMARKS"
                        + ", ja.RECORD_STATUS, ja.CREATED_BY, ja.UPDATED_BY"
                    + " FROM HRM_MP_JOB_SITE_AREAS ja"
                    + " WHERE ja.RECORD_STATUS not like @DeleteRecord"
                    + (areaInfo.Sequence > 0 ? " AND ja.JOB_SITE_AREA_SEQ = @Sequence" : "")
                    + (JobSiteSeq > 0 ? " AND ja.JOB_SITE_SEQ = @JobSiteSeq" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);                

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }
        public void Insert(JobSiteAreaInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@JobSiteSeq", Info.JobSites == null ? (int?)null : Info.JobSites.Sequence);
                dbManager.AddParameters(1, "@AreaSeq", Info.AreaName);
                dbManager.AddParameters(2, "@Remarks", Info.Remarks);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_JOB_SITE_AREAS(JOB_SITE_SEQ, AREA_NAME"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@JobSiteSeq, @AreaSeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(JobSiteAreaInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@JobSiteSeq", Info.JobSites == null ? (int?)null : Info.JobSites.Sequence);
                dbManager.AddParameters(2, "@AreaSeq", Info.AreaName);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_JOB_SITE_AREAS"
                           + " SET JOB_SITE_SEQ = @JobSiteSeq"
                                + ", AREA_NAME = @AreaSeq"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE JOB_SITE_AREA_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_JOB_SITE_AREAS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE JOB_SITE_AREA_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
